
const app = getApp<IAppOption>()
// pages/users/userInfo.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid:0,
    info:{},
    initEnable:false,
    postEnable:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var me = this;
    var userInfo={};
    var initEnable = me.data.initEnable;
    var postEnable = me.data.postEnable;
    if( options.uid==0 ){
      userInfo={
        grainId: app.globalData.userInfo.grainId,
        userCode:"",
        nickName:"",
        tel:"",
        adminFlag:0
      };
      initEnable = false;
      postEnable = true;
    }
    else{
      userInfo = app.globalData.currentUserInfo;
      initEnable = app.globalData.userInfo.adminFlag>0;
      postEnable = app.globalData.userInfo.adminFlag>0;
    }
    me.setData({
      uid:options.uid,
      info: userInfo,
      initEnable:initEnable ,
      postEnable:postEnable
    },()=>{
      console.log(me.data)
    })
  },
  // 事件处理函数
  onchange(ev) { 
    console.log(ev)
    var info = this.data.info;
    info[ev.currentTarget.dataset.inputkey] = ev.detail.value;
    this.setData({
      pwds:info
    }, ()=>{
      console.log(info)
    })
  },
  // 事件处理函数
  onSwitchAdminFlag(ev) { 
    console.log(ev)
    var info = this.data.info;
    info.adminFlag  = ev.detail.value?1:0;
    this.setData({
      info:info
    }, ()=>{
      console.log(info)
    })
  },
  acCancel() { 
    wx.navigateBack({delta:1});
  },
  acSave() { 
    var me = this;
    try { 
      var ps = me.data.info;
      var key = ps.userId && ps.userId>0 ?"custom/editUserInfo":"custom/newUserInfo";
      app.api.callApi( key, ps).then(res=>{ 
        console.log(res)
        if( res==1){ 
          wx.showModal({
            title:"登录账号",
            content:"提交成功",
            showCancel:false,
            success (res) {                
              wx.navigateBack({
                delta:1
              });
            }
          });
        }
        else{
          wx.showToast({
            icon:"error",
            title:"提交失败！",
            duration: 2000
          })
        }
      });
    } catch (error) {
      console.log(error);
    }
  },
  acInitPwd() { 
    var me = this;
    try { 
      wx.showModal({
        title:"初始化密码",
        content:"是否确定要初始化该账号的密码？",
        showCancel:true,
        success (res) {   
          if(res.confirm) {
            var ps = {
              userId: me.data.info.userId
            };
            var key =  "custom/initUserPwd" ;
            app.api.callApi( key, ps).then(res=>{ 
              console.log(res)
              if( res==1){ 
                wx.showModal({
                  title:"初始化密码",
                  content:"初始化密码完成",
                  showCancel:false,
                  success (res) {     
                  }
                });
              }
              else{
                wx.showToast({
                  icon:"error",
                  title:"初始化密码失败！",
                  duration: 2000
                })
              }
            });
          }
          
        }

      })
    } catch (error) {
      console.log(error);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})